//Inicjalizacja zmiennych
//Kalkulator czy symulator


package AS3.calculator {
import AS3.*;
import AS3.constants.Coefficients;
import AS3.constants.combo.Income;

import mx.controls.Alert;

import mx.core.FlexGlobals;

import zakladki.AS3.VehicleSelectedData;

public class CoefficientCalculator {

    private var mainApp:Fossilminator = FlexGlobals.topLevelApplication as Fossilminator;

    public function createCoefficientParams(chosenCountry:Country):CalculationParams {
        var result:CalculationParams = new CalculationParams();

        var richCoefficient:Number = calculateRichCoefficient();
        var consumptionCoefficient:Number = consLevel2CO2(chosenCountry.consumption2000,richCoefficient);

        result.heatGJCO2Coefficient = calcCOHeatGJCoefficient(chosenCountry);
        result.electricityGJCO2Coefficient = calcCOElectricityGJCoefficient(chosenCountry);
        result.electricityMWhCO2Coefficient = calcCOElectricityMWhCoefficient(chosenCountry);

        result.richCoefficient = richCoefficient;
        result.consumptionCoefficient = consumptionCoefficient;

        return result;
    }

    private function calcCOHeatGJCoefficient(chosenCountry:Country):Number{
       var coalHeatGJ:Number = Coefficients.COAL_HEAT_GJ;
       if(chosenCountry.nameEn == 'Estonia'){
          coalHeatGJ = coalHeatGJ * 1.4;
       }
       return chosenCountry.coalEnergyPercentage * coalHeatGJ + chosenCountry.oilEnergyPercentage * Coefficients.OIL_HEAT_GJ +
              chosenCountry.gasEnergyPercentage * Coefficients.GAS_HEAT_GJ + chosenCountry.nuclearEnergy * Coefficients.NUCLEAR_HEAT_GJ +
              chosenCountry.hydroEnergy * Coefficients.HYDRO_HEAT_GJ + chosenCountry.renewableEnergy * Coefficients.RENEWABLE_HEAT_GJ;
    }

    private function calcCOElectricityGJCoefficient(chosenCountry:Country):Number{
       var coalElectricityGJ:Number = Coefficients.COAL_ELECTRICITY_GJ;
       if(chosenCountry.nameEn == 'Estonia'){
          coalElectricityGJ = coalElectricityGJ * 1.4;
       }
       return chosenCountry.coalEnergyPercentage * coalElectricityGJ + chosenCountry.oilEnergyPercentage * Coefficients.OIL_ELECTRICITY_GJ +
              chosenCountry.gasEnergyPercentage * Coefficients.GAS_ELECTRICITY_GJ + chosenCountry.nuclearEnergy * Coefficients.NUCLEAR_ELECTRICITY_GJ +
              chosenCountry.hydroEnergy * Coefficients.HYDRO_ELECTRICITY_GJ + chosenCountry.renewableEnergy * Coefficients.RENEWABLE_ELECTRICITY_GJ;
    }

    private function calcCOElectricityMWhCoefficient(chosenCountry:Country):Number{
       var coalElectricityMWh:Number = Coefficients.COAL_ELECTRICITY_MWh;
       if(chosenCountry.nameEn == 'Estonia'){
          coalElectricityMWh = coalElectricityMWh * 1.4;
       }
       return chosenCountry.coalEnergyPercentage * coalElectricityMWh + chosenCountry.oilEnergyPercentage * Coefficients.OIL_ELECTRICITY_MWh +
              chosenCountry.gasEnergyPercentage * Coefficients.GAS_ELECTRICITY_MWh + chosenCountry.nuclearEnergy * Coefficients.NUCLEAR_ELECTRICITY_MWh +
              chosenCountry.hydroEnergy * Coefficients.HYDRO_ELECTRICITY_MWh + chosenCountry.renewableEnergy * Coefficients.RENEWABLE_ELECTRICITY_MWh;
    }


    public function consLevel2CO2(consumption:Number, richCoefficient:Number):Number {
        //return 0.625 * (Math.log((consumption * richCoefficient + 1500) / 100) - 2.7);
        return 0.02291 * (Math.pow(consumption * richCoefficient/100,0.82));
    }

    public function calculateGrowthCoefficient(year:int, chosenCountry:Country, economicGrowth:Number):Number {

        var IYear:int;
        var IYPop:int;

        var Year:Date = new Date();
        var GDPPercGrowth:Number;
        var PopPercGrowth:Number;
        var IY:Number;
        var P1:Number;
        var P2:Number;
        var GrowthCoeff:Number;
        var EconomicGrowthA:Number;

        EconomicGrowthA = 0.75;
        if (economicGrowth == 1) {
            EconomicGrowthA = 1.00;
        }
        if (economicGrowth == 2) {
            EconomicGrowthA = 1.25;
        }
        if (economicGrowth == 3) {
            EconomicGrowthA = 1.50;
        }
        if (economicGrowth == 4) {
            EconomicGrowthA = 1.75;
        }

        if (year == 0) {
            year = Year.getFullYear();
        }
        if (year < 2004) {
            year = 2004;
        }
        for (IYear = 2004; IYear <= year; IYear++) {
            IY = IYear;
            GDPPercGrowth = 1 + (0.4 + 21000 / (chosenCountry.consumption2000 + 5000)) / 100 * EconomicGrowthA;
            if (chosenCountry.nameEn == 'China') {
                GDPPercGrowth = 1 + (GDPPercGrowth - 1) * (200 / (IY - 1900));

            }
            IYPop = (IYear - 2000) / 10;
            P1 = chosenCountry.getPopulationForYear(IYPop) + (chosenCountry.getPopulationForYear(IYPop + 1) - chosenCountry.getPopulationForYear(IYPop)) / 10 * (IYear - (2000 + 10 * IYPop));
            IYPop = (IYear + 1 - 2000) / 10;
            P2 = chosenCountry.getPopulationForYear(IYPop) + (chosenCountry.getPopulationForYear(IYPop + 1) - chosenCountry.getPopulationForYear(IYPop)) / 10 * (IYear + 1 - (2000 + 10 * IYPop));
            PopPercGrowth = P2 / P1;
            if (PopPercGrowth < 1) // starzejące się społeczeństwa nie bogacą się
            {
                PopPercGrowth = 1;
            }
            GrowthCoeff = GDPPercGrowth / PopPercGrowth;
        }

        return GrowthCoeff;
    }



    public function calculateRichCoefficient():Number {
        var richCoefficient:Number = 1.0;
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_10_PER_CENT) {
            richCoefficient = 0.05;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_20_PER_CENT) {
            richCoefficient = 0.20;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_50_PER_CENT) {
            richCoefficient = 0.50;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_80_PER_CENT) {
            richCoefficient = 0.80;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE) {
            richCoefficient = 1.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_120_PER_CENT) {
            richCoefficient = 1.20;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_150_PER_CENT) {
            richCoefficient = 1.50;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_200_PER_CENT) {
            richCoefficient = 2.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_300_PER_CENT) {
            richCoefficient = 3.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_500_PER_CENT) {
            richCoefficient = 5.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_10) {
            richCoefficient = 10.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_20) {
            richCoefficient = 20.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_50) {
            richCoefficient = 50.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_100) {
            richCoefficient = 100.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_200) {
            richCoefficient = 200.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_500) {
            richCoefficient = 500.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_1000) {
            richCoefficient = 1000.00;
        }

        return richCoefficient;

    }



















}


}

